package com.coffeeloversupportlibrary.coffeelover.mercury.my.exchangerecord;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.ShopRecord;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.adapter.RecordAdapter;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/25 20:15
 * @projectName TurningCoffee
 * @className ExChangeRecordActivity.java
 * @description TODO
 */
public class ExChangeRecordActivity extends AppCompatActivity {
    private SharedPreferences userInfo;
    private Activity activity;

    private ListView lv_shoprecord;
    private ShopRecord shopRecord;
    private User user;

    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_record);

        initViews();
        getData();
    }

    void getData(){
        Call call = httpUtils.getCall("/user/center-mall-user/exchangeList/"+user.getData().getUser().getUserId());
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("ExChangeRecord", "onResponse: "+res );
                shopRecord = jsonUtils.getShopRecard(res);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RecordAdapter adapter = new RecordAdapter(activity, shopRecord.getData().getExchangeList());
                        lv_shoprecord.setAdapter(adapter);
                    }
                });
            }
        });
    }

    void initViews(){
        httpUtils = new HttpUtils();
        jsonUtils = new JsonUtils();
        activity = ExChangeRecordActivity.this;

        userInfo = getSharedPreferences("user", 0);
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));

        lv_shoprecord = findViewById(R.id.lv_detailedshop);
    }

}
